package com.ruoyi.web.controller.wordcloud;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wordcloud.domain.TextP;
import com.ruoyi.wordcloud.service.ITextPService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/wordcloud/distr")
public class DistrController extends BaseController
{
    @Autowired
    private ITextPService textPService;

    /**
     * 查询展示词云图列表
     */
    @PreAuthorize("@ss.hasPermi('wordcloud:distr:list')")
    @GetMapping("/list")
    public TableDataInfo list(TextP textP)
    {
        startPage();
        List<TextP> list = textPService.selectTextPList(textP);
        return getDataTable(list);
    }

    /**
     * 导出展示词云图列表
     */
    @PreAuthorize("@ss.hasPermi('wordcloud:distr:export')")
    @Log(title = "展示词云图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TextP textP)
    {
        List<TextP> list = textPService.selectTextPList(textP);
        ExcelUtil<TextP> util = new ExcelUtil<TextP>(TextP.class);
        util.exportExcel(response, list, "展示词云图数据");
    }

    /**
     * 获取展示词云图详细信息
     */
    @PreAuthorize("@ss.hasPermi('wordcloud:distr:query')")
    @GetMapping(value = "/{tpId}")
    public AjaxResult getInfo(@PathVariable("tpId") Long tpId)
    {
        return success(textPService.selectTextPByTpId(tpId));
    }

    /**
     * 新增展示词云图
     */
    @PreAuthorize("@ss.hasPermi('wordcloud:distr:add')")
    @Log(title = "展示词云图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TextP textP)
    {
        try {
            return toAjax(textPService.insertTextP(textP));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改展示词云图
     */
    @PreAuthorize("@ss.hasPermi('wordcloud:distr:edit')")
    @Log(title = "展示词云图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TextP textP)
    {
        return toAjax(textPService.updateTextP(textP));
    }

    /**
     * 删除展示词云图
     */
    @PreAuthorize("@ss.hasPermi('wordcloud:distr:remove')")
    @Log(title = "展示词云图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tpIds}")
    public AjaxResult remove(@PathVariable Long[] tpIds)
    {
        return toAjax(textPService.deleteTextPByTpIds(tpIds));
    }
}
